package com.xyy.saas.payment.adpater.jdCredit.resp;
import com.xyy.saas.payment.adpater.jdCredit.enums.CodeEnum;
import com.xyy.saas.payment.adpater.jdCredit.util.DateUtil;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.Date;
import java.util.Map;

/**
 * API响应基础出参
 *
 * @author yuchengying
 * @date 2023/11/29
 */
@Data
public class ApiResponse<T> implements Serializable {
    /**
     * 请求编号
     */
    private String reqNo;
    /**
     * 响应日期
     */
    private String resDate;
    /**
     * 响应码
     */
    private String resCode;
    /**
     * 响应信息
     */
    private String resDesc;
    /**
     * 响应体
     */
    private T data;
    /**
     * ext 扩展字段.
     */
    private Map<String, Object> ext;


    /**
     * 是否成功
     *
     * @return boolean
     */
    public boolean isSuccess() {
        return CodeEnum.SUCCESS.getCode().equals(resCode);
    }

    /**
     * 失败响应
     *
     * @return {@link ApiResponse}<{@link T}>
     */
    public static <T> ApiResponse<T> fail(String reqNo, String errorCode, String errorMessage) {
        return buildFailed(reqNo, errorCode, errorMessage);
    }

    /**
     * 失败响应
     *
     * @return {@link ApiResponse}<{@link T}>
     */
    public static <T> ApiResponse<T> failMsg(String errorCode, String errorMessage) {
        return buildFailed(null, errorCode, errorMessage);
    }

    public static <T> ApiResponse<T> buildFailed(String reqNo, String errorCode, String errorMessage) {
        return of2(reqNo, errorCode, errorMessage, null);
    }

    private static <T> ApiResponse<T> of2(String reqNo, String code, String msg, T data) {
        ApiResponse<T> response = new ApiResponse<>();
        if(StringUtils.isBlank(reqNo)) {
            response.setReqNo(DateUtil.formatDateString(new Date(), DateUtil.DATEPATTERN));
        } else {
            response.setReqNo(reqNo);
        }
        response.setResDate(DateUtil.formatDateString(new Date(), DateUtil.DATEPATTERN));
        response.setData(data);
        response.setResCode(code);
        response.setResDesc(msg);
        return response;
    }

    /**
     * 构建error
     *
     * @param reqNo 请求编号
     * @param code  编码
     * @param msg   消息
     * @return {@link ApiResponse }<{@link T }>
     */
    public static <T> ApiResponse<T> buildError(String reqNo, String code, String msg) {
        ApiResponse<T> response = new ApiResponse<>();
        if(StringUtils.isBlank(reqNo)) {
            response.setReqNo(DateUtil.formatDateString(new Date(), DateUtil.DATEPATTERN));
        } else {
            response.setReqNo(reqNo);
        }
        response.setResDate(DateUtil.formatDateString(new Date(), DateUtil.DATEPATTERN));
        response.setResCode(code);
        response.setResDesc(msg);
        return response;
    }

    /**
     * 构建error
     *
     * @param reqNo 请求编号
     * @param code  编码
     * @param msg   消息
     * @return {@link ApiResponse }<{@link T }>
     */
    public static <T> ApiResponse<T> build(String reqNo, String code, String msg, T data) {
        ApiResponse<T> response = new ApiResponse<>();
        if(StringUtils.isBlank(reqNo)) {
            response.setReqNo(DateUtil.formatDateString(new Date(), DateUtil.DATEPATTERN));
        } else {
            response.setReqNo(reqNo);
        }
        response.setResDate(DateUtil.formatDateString(new Date(), DateUtil.DATEPATTERN));
        response.setResCode(code);
        response.setResDesc(msg);
        response.setData(data);
        return response;
    }
    /**
     * 成功响应
     *
     * @param data 数据
     * @return {@link ApiResponse}<{@link T}>
     */
    public static <T> ApiResponse<T> success(T data) {
        return of(CodeEnum.SUCCESS.getCode(), CodeEnum.SUCCESS.getMsg(), data);
    }

    /**
     * 成功响应
     *
     * @return {@link ApiResponse }<{@link T }>
     */
    public static <T> ApiResponse<T> success() {
        return of(CodeEnum.SUCCESS.getCode(), CodeEnum.SUCCESS.getMsg(), null);
    }

    /**
     * 成功响应
     *
     * @return {@link ApiResponse }<{@link T }>
     */
    public static <T> ApiResponse<T> success(String reqNo) {
        return of(reqNo, CodeEnum.SUCCESS.getCode(), CodeEnum.SUCCESS.getMsg(), null);
    }

    /**
     * 成功响应
     *
     * @return {@link ApiResponse }<{@link T }>
     */
    public static <T> ApiResponse<T> success(String reqNo, T data) {
        return of(reqNo, CodeEnum.SUCCESS.getCode(), CodeEnum.SUCCESS.getMsg(), data);
    }

    /**
     * @param code 编码
     * @param msg  详情
     * @param data 数据
     * @return {@link ApiResponse}<{@link T}>
     */
    public static <T> ApiResponse<T> of(String code, String msg, T data) {
        ApiResponse<T> response = new ApiResponse<>();
        response.setData(data);
        response.setResCode(code);
        response.setResDesc(msg);
        return response;
    }

    /**
     * @param reqNo 请求编号
     * @param code  编码
     * @param msg   详情
     * @param data  数据
     * @return {@link ApiResponse }<{@link T }>
     */
    public static <T> ApiResponse<T> of(String reqNo, String code, String msg, T data) {
        ApiResponse<T> response = new ApiResponse<>();
        response.setReqNo(reqNo);
        response.setResDate(DateUtil.formatDateString(new Date(), DateUtil.DATEPATTERN));
        response.setData(data);
        response.setResCode(code);
        response.setResDesc(msg);
        return response;
    }

    /**
     * @param code 编码
     * @param data 数据
     * @return {@link ApiResponse}<{@link T}>
     */
    public static <T> ApiResponse<T> of(CodeEnum code, T data) {
        ApiResponse<T> response = new ApiResponse<>();
        response.setData(data);
        response.setResCode(code.getCode());
        response.setResDesc(code.getMsg());
        return response;
    }
}
